﻿using System;
using PairingMatrix.Common.Command;

namespace PairingMatrix.ViewModel
{
    public class ToolBarViewModel : ViewModelBase
    {
        private readonly CardWallViewModel _cardWallViewModel;

        public ToolBarViewModel(CardWallViewModel cardWallViewModel)
        {
            this._cardWallViewModel = cardWallViewModel;
            _cardWallViewModel.PropertyChanged += _cardWallViewModel_PropertyChanged;
        }

        void _cardWallViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentInstanceDate")
            {
                SyncWithMingleCommand.RaiseCanExecuteChanged();
                AllowMingleSync = SyncWithMingleCommand.CanExecute(null);
            }

            if(e.PropertyName == "IsSyncingAllProjectCards")
                OnPropertyChanged("IsSyncingAllProjectCards");
        }


        public bool IsSyncingAllProjectCards
        {
            get { return _cardWallViewModel.IsSyncingAllProjectCards; }
        }


        private RelayCommand _addProjectCardCommand;
        public RelayCommand AddProjectCardCommand
        {
            get {
                return _addProjectCardCommand ?? (_addProjectCardCommand = new RelayCommand(param => _cardWallViewModel.AddProjectCardCommand.Execute(param)));
            }
        }

        private RelayCommand _newTeamMemberCommand;
        public RelayCommand NewTeamMemberCommand
        {
            get {
                return _newTeamMemberCommand ?? (_newTeamMemberCommand = new RelayCommand(param => _cardWallViewModel.NewTeamMemberCommand.Execute(null)));
            }
        }

        private RelayCommand _showPairingMatrixCommand;
        public RelayCommand ShowPairingMatrixCommand
        {
            get {
                return _showPairingMatrixCommand ?? (_showPairingMatrixCommand = new RelayCommand(param => _cardWallViewModel.ShowPairingMatrixCommand.Execute(null)));
            }
        }

        private RelayCommand _showIceCreamMeterCommand;
        public RelayCommand ShowIceCreamMeterCommand
        {
            get {
                return _showIceCreamMeterCommand ?? (_showIceCreamMeterCommand = new RelayCommand(param => _cardWallViewModel.ShowIceCreamMeterCommand.Execute(null)));
            }
        }

        private RelayCommand _showSettingsCommand;
        public RelayCommand ShowSettingsCommand
        {
            get {
                return _showSettingsCommand ?? (_showSettingsCommand = new RelayCommand(param => _cardWallViewModel.ShowSettingsCommand.Execute(null)));
            }
        }

        private RelayCommand _syncWithMingleCommand;
        public RelayCommand SyncWithMingleCommand
        {
            get {
                return _syncWithMingleCommand ?? (_syncWithMingleCommand = new RelayCommand(param => _cardWallViewModel.SyncWithMingleCommand.Execute(null), param => _cardWallViewModel.CurrentInstanceDate.Date >= DateTime.Today));
            }
        }

        private RelayCommand _saveCommand;
        public RelayCommand SaveCommand
        {
            get {
                return _saveCommand ?? (_saveCommand = new RelayCommand(param => _cardWallViewModel.SaveCommand.Execute(null)));
            }
        }

        private bool _allowMingleSync;
        public bool AllowMingleSync
        {
            get { return _allowMingleSync; }
            set
            {
                if (_allowMingleSync == value)
                    return;
                _allowMingleSync = value;
                OnPropertyChanged("AllowMingleSync");
            }
        }
        
    }
}